package com.zyd.thread;

import com.zyd.dao.SocketNowBlackCarDao;
import com.zyd.io.model.SocketNowEntity;
import com.zyd.io.model.SocketNowEntityExample;
import com.zyd.mappers.SocketNowEntityMapper;
import com.zyd.util.UUIDUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 黑车业务多线程处理类
 */
@Component
public class BlackCarThread {

    @Autowired
    private SocketNowEntityMapper socketNowEntityMapper;


    @Autowired
    private SocketNowBlackCarDao dao;

    /**
     * 根据标签id 查询轨迹数据然后将轨迹数据插入到黑车轨迹表
     * @param tagId 标签id
     */
    @Async//该方法受spring线程管理
    public void addBlackCar(String tagId) {
        System.out.println("新增黑车轨迹到黑车轨迹表中线程开始：tagid:"+tagId);
        SocketNowEntityExample example =new SocketNowEntityExample();
        SocketNowEntityExample.Criteria criteria = example.createCriteria();
        criteria.andTag_idEqualTo(tagId);
        List<SocketNowEntity> socketNowEntities = socketNowEntityMapper.selectByExample(example);
        for (SocketNowEntity socketNowEntity : socketNowEntities) {
             System.out.println("新增黑车轨迹到黑车轨迹表中线程进行中");
//            socketNowEntity.setId(Long.parseLong(UUIDUtil.getUUid()));//生成新的id
            dao.save(socketNowEntity);
        }
        System.out.println("新增黑车轨迹到黑车轨迹表中线程结束");

    }

    /**
     * 根据标签id删除对应的黑车轨迹数据
     * @param tagcode
     */
    @Async//该方法受spring线程管理
    public void delete(String tagcode) {
        System.out.println("删除黑车轨迹从黑车轨迹表中线程开始：tagcode:"+tagcode);
        dao.deleteByTagId(tagcode);
        System.out.println("删除黑车轨迹从黑车轨迹表中线程结束");

    }
}
